<template>
  <div class="realm-info-lay">
    <el-form :model="platSetting" ref="refForm" :rules="rules">
      <el-form-item label="仅单用户登录" :label-width="labelWid">
        <el-checkbox v-model="platSetting.$singleLogin">

        </el-checkbox>
      </el-form-item>
      <el-form-item label="单用户登录终端" :label-width="labelWid">
        <el-input type="textarea" v-model="platSetting.$singleLoginTerminals"
                  :disabled="platSetting.$singleLogin === true" :rows="5" />
        每个用户在每个平台同一个终端只允许登录一次，多个终端用,隔开
      </el-form-item>
    </el-form>
    <div class="realm-footer">
      <el-button type="danger" @click="onCancel">取消</el-button>
      <el-button type="primary" style="margin-left: 32px;" @click="onSave">保存</el-button>
    </div>
  </div>
</template>

<script setup lang="ts">
import {ref, onMounted, reactive} from "vue";
import {useStore} from "@/stores";
import {loadAttributeOfAsset, lookupRealm, saveAttributeOfAsset, saveRealm} from "@/apis";
import {msgError, msgInfo} from "@/utils/InfoUtil";
import {SERVERS} from "@/stores/conf";

const store: any = useStore();

const labelWid = ref(130);
const oldInfo = ref();
const refForm = ref();
const platSetting = ref({});

const rules = reactive({
})

const onCancel = () => {
  platSetting.value = {...oldInfo.value};
}

const onSave = async () => {
  if (await refForm.value.validate()) {
    const platId: any = store.getCurrentPlatform().id;
    const result = await saveAttributeOfAsset({
      server: SERVERS.PLATFORM,
      targetId: platId,
      assetType: 'PLATFORM',
      props: platSetting.value
    });
    if (result.code === 0) {
      msgInfo('保存平台设置成功！');
      oldInfo.value = {...platSetting.value};
    } else {
      msgError(result.message);
    }
  }
}

const refresh = async () => {
  const platId: any = store.getCurrentPlatform().id;
  const result: any = await loadAttributeOfAsset({
    server: SERVERS.PLATFORM,
    targetId: platId,
    assetType: 'PLATFORM'
  });
  platSetting.value = {};
  if (result && result.data) {
    platSetting.value.$singleLogin = result.data.$singleLogin || false;
    platSetting.value.$singleLoginTerminals = result.data.$singleLoginTerminals || '';
  }

  oldInfo.value = {...platSetting.value};
}

onMounted(() => {
  refresh();
})

defineExpose({
  refresh,
})

</script>

<style scoped>
.realm-info-lay {
  display: flex;
  flex-direction: column;
  margin: 16px 32px;
  justify-content: flex-start;
  width: 50%;
}

.inherit-item {
  display: flex;
  flex-direction: row;
  margin: 0 8px;
  justify-items: center;
  align-items: center;
}

.realm-footer {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  margin: 16px 0;
}
</style>
